package my.climbing;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import my.map.ClimbingMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.*;
import android.widget.*;

public class SelectPathActivity extends ListActivity{
	ImageView iv;
	ArrayList<String> list=new ArrayList<String>();
	private int mSelectedRow = 0;
	String strMt;
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selectpath);
		
		Intent i = getIntent();
		strMt = i.getStringExtra("mountain");
		downLoadJSON(strMt);
		setListAdapter(new MyPathListAdapter(this, R.layout.selectpathrow, list));
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		
		Intent i = new Intent();
		i.setClass(this, ClimbingMap.class);
		i.putExtra("mountain", strMt);
		i.putExtra("path", list.get(position));
		
		startActivity(i);
	}
	
	public void downLoadJSON(String mountain)
	{
		String json="";
		try {
			HttpClient hc = new DefaultHttpClient();
			HttpPost hp = new HttpPost("http://211.238.142.88:8080/MapPointProject/map/path_list.jsp");
			
			List data = new ArrayList();
			data.add(new BasicNameValuePair("mountain", mountain));
			hp.setEntity(new UrlEncodedFormEntity(data,"UTF-8"));
			
			HttpResponse hr = hc.execute(hp);
			
			HttpEntity entity = hr.getEntity();
			if (entity != null){
				String str="";
				InputStream is = entity.getContent();
				BufferedReader in=new BufferedReader(
						new InputStreamReader(is));
				while(true)
				{
					str=in.readLine();
						if(str==null)
							break;
						json+=str;
				}
				in.close();
   
				JSONArray ja=new JSONArray(json);
				for(int i=0;i<ja.length();i++)
				{
					JSONObject obj=ja.getJSONObject(i);
					String strPath = obj.getString("path");
	   
					list.add(strPath);
				}
			}
			
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}
	
}

class MyPathListAdapter extends BaseAdapter
{

	ArrayList<String> list;
	int layout;
	LayoutInflater in;
	Context mc;
	
	public MyPathListAdapter(Context c,int lay,ArrayList<String> arr)
	{
		list=arr;
		layout=lay;
		mc=c;
		in=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
	}
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup viewGroup) {
		// TODO Auto-generated method stub
		if(convertView==null)
		{
			convertView=in.inflate(layout, null);
		}
		String mt = list.get(pos);
		TextView tv = (TextView)convertView.findViewById(R.id.list_path);
		tv.setText(mt);
		
		return convertView;
	}
	
}
